﻿using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.SubForm.Editor {
    public partial class PurchaseLineEdit : Form {
        private static string SqlStrMain = "SELECT [ID],[Date],[State] FROM [Purchase]";
        private static string SqlStrSub = "SELECT [ID],[GoodsID],[Amount],[SupplyID] FROM [PurchaseDetail]";
        private static string SqlStrDisplay = "SELECT [PurchaseDetail].[ID],[Goods].[Name] [GoodsName],[Amount],[Supply].[Name] [SupplyName] FROM [PurchaseDetail] JOIN [Goods] ON [Goods].[ID] = [PurchaseDetail].[GoodsID] JOIN [Supply] ON [Supply].[ID] = [PurchaseDetail].[SupplyID]";//Todo
        public int PurchaseID = -1;
        public int PurchaseDetailID = -1;
        private static ArrayList selection1 = new ArrayList();
        private static ArrayList selection2 = new ArrayList();
        public PurchaseLineEdit() {
            InitializeComponent();
        }

        private void PurchaseLineEdit_Load(object sender, EventArgs e) {
            DataTable Goods = DBHelper.GetDataTable("SELECT * FROM [Goods]");
            for (int i = 0; i < Goods.Rows.Count; i++) {
                selection1.Add(new DictionaryEntry(Goods.Rows[i].ItemArray[0].ToString(), Goods.Rows[i].ItemArray[1].ToString()));
            }
            comboBox1.DataSource = selection1;
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember = "Key";
            comboBox1.SelectedIndex = 0;
            DataTable Supply = DBHelper.GetDataTable("SELECT * FROM [Supply]");
            for (int i = 0; i < Supply.Rows.Count; i++) {
                selection2.Add(new DictionaryEntry(Supply.Rows[i].ItemArray[0].ToString(), Supply.Rows[i].ItemArray[1].ToString()));
            }
            comboBox2.DataSource = selection2;
            comboBox2.DisplayMember = "Value";
            comboBox2.ValueMember = "Key";
            comboBox2.SelectedIndex = 0;
            textBox2.Text = "未通过审核";
            if (PurchaseID != -1) {
                //
                DataTable mainTable = DBHelper.GetDataTable($"{SqlStrMain} WHERE [ID] = '{PurchaseID}'");
                textBox1.Text = mainTable.Rows[0].ItemArray[0].ToString();
                dateTimePicker1.Value = DateTime.Parse(mainTable.Rows[0].ItemArray[1].ToString());
                textBox2.Text = mainTable.Rows[0].ItemArray[2].ToString().Equals("true") ? "已通过审核" : "未通过审核";
                DataTable subTable = DBHelper.GetDataTable(SqlStrSub);
                dataGridView1.DataSource = DBHelper.GetDataTable($"{SqlStrDisplay} WHERE [PurchaseDetail].[ID] = {PurchaseID}");
            }
            dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
        }
        //修改功能
        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {
            PurchaseDetailID = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            for(int i = 0; i < selection1.Count; i++) {
                DictionaryEntry item = (DictionaryEntry)selection1[i];
                if (item.Value.Equals(dataGridView1.CurrentRow.Cells[1].Value.ToString()))
                    comboBox1.SelectedIndex = i;
            }
            numericUpDown1.Value = (int)dataGridView1.CurrentRow.Cells[2].Value;
            for (int i = 0; i < selection2.Count; i++) {
                DictionaryEntry item = (DictionaryEntry)selection2[i];
                if (item.Value.Equals(dataGridView1.CurrentRow.Cells[3].Value.ToString()))
                    comboBox2.SelectedIndex = i;
            }
            button1.Text = "修改";
        }
        //删除功能
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
            int ID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            //删除前确认
            DialogResult res = MessageBox.Show($"你确定要删除ID为{ID}的数据？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (res == DialogResult.OK) {
                //与运算取结果    与运算:一假全假
                bool result = true;
                result &= DBHelper.ExecuteNonQuery($"DELETE FROM [PurchaseDetail] WHERE [PurchaseID] = {ID}");
                result &= DBHelper.ExecuteNonQuery($"DELETE FROM [Purchase] WHERE [ID] = {ID}");
                if (result) {
                    MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                } else {
                    MessageBox.Show("删除失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            dataGridView1.DataSource = DBHelper.GetDataTable($"{SqlStrDisplay} WHERE [PurchaseDetail].[ID] = {PurchaseID}");
        }
        //新增功能
        private void button1_Click(object sender, EventArgs e) {
            if (button1.Text.Equals("新增")) {
                bool result = true;
                if (textBox1.Text.Equals(string.Empty)) {
                    result &= DBHelper.ExecuteNonQuery($"INSERT [Purchase] VALUES(CAST('{dateTimePicker1.Value.ToString("yyyy-MM-dd")}' AS DATE),0,0)");
                    DataTable tmp = DBHelper.GetDataTable("SELECT MAX(ID) FROM [Purchase]");
                    PurchaseID = (int)tmp.Rows[0][0];
                    textBox1.Text = PurchaseID.ToString();
                }
                result &= DBHelper.ExecuteNonQuery($"INSERT [PurchaseDetail] VALUES({PurchaseID},{comboBox1.SelectedValue},{numericUpDown1.Value},{comboBox2.SelectedValue})");
                if (!result) MessageBox.Show("添加失败！");
            } else {
                bool result = DBHelper.ExecuteNonQuery($"UPDATE [PurchaseDetail] SET [GoodsID] = {comboBox1.SelectedValue},[Amount] = {numericUpDown1.Value},[SupplyID] = {comboBox2.SelectedValue} WHERE [ID] = {PurchaseDetailID}");
                if (!result) MessageBox.Show("修改失败！");
            }
            button1.Text = "新增";
            dataGridView1.DataSource = DBHelper.GetDataTable($"{SqlStrDisplay} WHERE [PurchaseDetail].[ID] = {PurchaseID}");
        }
        //重置功能
        private void button2_Click(object sender, EventArgs e) {
            comboBox1.SelectedIndex = 0;
            numericUpDown1.Value = 1;
            comboBox2.SelectedIndex = 0;
            button1.Text = "新增";
        }
        //鼠标点击时选中数据表项
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
            if (e.RowIndex > 0 && e.RowIndex < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
            if (PurchaseID != -1) {
                if (DBHelper.ExecuteNonQuery($"UPDATE [Purchase] SET [Date] = CAST('{dateTimePicker1.Value.ToString("yyyy-MM-dd")}' AS DATE) WHERE [ID] = {PurchaseID}")) {
                    MessageBox.Show("修改日期成功！");
                } else {
                    MessageBox.Show("修改日期失败！");
                }
            }
        }
    }
}
